package com.controller.NewsAjax;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.entity.ChanPing;
import com.entity.XinWenZiXun;
import com.entity.YongHu;
import com.entity.YongHuHangYe;
import com.service.CpService;
import com.service.UserIndustryService;
import com.service.UserService;
import com.service.XwlmService;
import com.service.XwzxService;

@Controller
@RequestMapping("/frontend/manager")
public class NewAjaxController {
	@Autowired
	private XwlmService xwlmservice;
	@Autowired
	private XwzxService xwzxservice;
	@Autowired
	private CpService cpservice;
	@Autowired
	private UserService yhService;
	@Autowired
	private UserIndustryService uiService;
	@Autowired
	private CpService cpService;
	@RequestMapping("findPages")
	@ResponseBody
	public AjaxRes findPages(XinWenZiXun o,Page<XinWenZiXun> page){
		AjaxRes a=new AjaxRes();
		Page<XinWenZiXun> pages=xwzxservice.findByPage(o, page);
		System.out.println(pages);
		a.setSucceed(pages,"数据加载成功");
		return  a;
	}
	@RequestMapping("findPages1")
	@ResponseBody
	public AjaxRes findPages1(ChanPing o,Page<ChanPing> page){
		AjaxRes a=new AjaxRes();
		Page<ChanPing> pages=cpservice.findByPage(o, page);
		System.out.println(pages);
		a.setSucceed(pages,"数据加载成功");
		return  a;
	}
	@RequestMapping("/to_login")
	@ResponseBody
	public AjaxRes toLogin(HttpServletRequest request,YongHu yh){
		AjaxRes res=new AjaxRes();
		// String pwd = Md5.getPwd(yh.getPhone());
	//	 yh.setPhone(pwd);
		int i=yhService.countPWD(yh);
		System.out.println(yh.getPhone());
		if(i<=0){
			res.setFailMsg("登陆失败");
			return res;
		}
			res.setSucceedMsg("登陆成功");
			List<YongHu> yhlist=yhService.find(yh);
			request.getSession().setAttribute("yh", yhlist.get(0));
			return res;

	}
	@RequestMapping("/addcp")
	@ResponseBody
	public AjaxRes addcp(ChanPing cp,String fujianId){
		AjaxRes res = new AjaxRes();
		    YongHuHangYe yhhy=new YongHuHangYe();
			yhhy.setHymc(cp.getCpfl());
			System.out.println("---------"+yhhy);
			yhhy=uiService.findByid(yhhy);
			System.out.println("_________________"+yhhy);
			  Date dt = new Date(); 
			 String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(dt);
			cp.setCpfbsj(dateStr);
			cp.setShsj(dateStr);
			cp.setCpfl(yhhy.getId());
			cpService.insertFj(cp,fujianId);
			res.setResMsg("添加成功");
		
		return res;	
		
	}
	
}
